package com.huawo.opensdk.bean;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

public class DeviceUpgradeInfo {
    public Long build;
    public String version;
    public Boolean forceUpdate;
    public String upgradeContent;
    public List<Firmware> firmwares;

    public Resource resource;
    public String updateContentKey;

    public static JSONObject toJsonObject(DeviceUpgradeInfo deviceUpgradeInfo) {
        Map<String, Object> jsonMap = new HashMap<>();
        if (deviceUpgradeInfo != null) {
            jsonMap.put("build", deviceUpgradeInfo.build);
            jsonMap.put("version", deviceUpgradeInfo.version);
            jsonMap.put("forceUpdate", deviceUpgradeInfo.forceUpdate);
            jsonMap.put("upgradeContent", deviceUpgradeInfo.upgradeContent);
            List<JSONObject> firmwareJsonObjectList = new ArrayList<>();
            if (deviceUpgradeInfo.firmwares != null) {
                for (Firmware firmware : deviceUpgradeInfo.firmwares) {
                    firmwareJsonObjectList.add(Firmware.toJsonObject(firmware));
                }
            }
            jsonMap.put("firmwares", firmwareJsonObjectList);
        }
        return new JSONObject(jsonMap);
    }

    public static DeviceUpgradeInfo fromJsonObject(JSONObject jsonObject) {
        DeviceUpgradeInfo deviceUpgradeInfo = new DeviceUpgradeInfo();
        if (jsonObject != null) {
            deviceUpgradeInfo.build = jsonObject.optLong("build");
            deviceUpgradeInfo.version = jsonObject.optString("version");
            deviceUpgradeInfo.forceUpdate = jsonObject.optBoolean("forceUpdate");
            deviceUpgradeInfo.upgradeContent = jsonObject.optString("upgradeContent");
            if (jsonObject.has("firmwares")) {
                JSONArray firmwareJsonArray = jsonObject.optJSONArray("firmwares");
                if (firmwareJsonArray != null) {
                    List<Firmware> firmwareList = new ArrayList<>();
                    for (JSONObject firmwareJsonObject : new JsonArrayIterable(firmwareJsonArray)) {
                        firmwareList.add(Firmware.fromJsonObject(firmwareJsonObject));
                    }
                    deviceUpgradeInfo.firmwares = firmwareList;
                }
            }
        }
        return deviceUpgradeInfo;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        DeviceUpgradeInfo that = (DeviceUpgradeInfo) o;
        return Objects.equals(build, that.build) && Objects.equals(version, that.version) && Objects.equals(forceUpdate, that.forceUpdate) && Objects.equals(upgradeContent, that.upgradeContent) && Objects.equals(firmwares, that.firmwares);
    }

    @Override
    public int hashCode() {
        return Objects.hash(build, version, forceUpdate, upgradeContent, firmwares);
    }

    @Override
    public String toString() {
        return "DeviceUpgradeInfo{" +
                "build=" + build +
                ", version='" + version + '\'' +
                ", forceUpdate=" + forceUpdate +
                ", upgradeContent='" + upgradeContent + '\'' +
                ", firmwares=" + firmwares +
                '}';
    }

    public static class Firmware {
        public String id;
        public String name;
        public Integer type;
        public String url;
        public String md5;

        public static JSONObject toJsonObject(Firmware firmware) {
            Map<String, Object> jsonMap = new HashMap<>();
            if (firmware != null) {
                jsonMap.put("id", firmware.id);
                jsonMap.put("name", firmware.name);
                jsonMap.put("type", firmware.type);
                jsonMap.put("url", firmware.url);
                jsonMap.put("md5", firmware.md5);
            }
            return new JSONObject(jsonMap);
        }

        public static Firmware fromJsonObject(JSONObject jsonObject) {
            Firmware firmware = new Firmware();
            if (jsonObject != null) {
                firmware.id = jsonObject.optString("id");
                firmware.name = jsonObject.optString("name");
                firmware.type = jsonObject.optInt("type");
                firmware.url = jsonObject.optString("url");
                firmware.md5 = jsonObject.optString("md5");
            }
            return firmware;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o) return true;
            if (o == null || getClass() != o.getClass()) return false;
            Firmware firmware = (Firmware) o;
            return Objects.equals(id, firmware.id) && Objects.equals(name, firmware.name) && Objects.equals(type, firmware.type) && Objects.equals(url, firmware.url) && Objects.equals(md5, firmware.md5);
        }

        @Override
        public int hashCode() {
            return Objects.hash(id, name, type, url, md5);
        }

        @Override
        public String toString() {
            return "Firmware{" +
                    "id='" + id + '\'' +
                    ", name='" + name + '\'' +
                    ", type=" + type +
                    ", url='" + url + '\'' +
                    ", md5='" + md5 + '\'' +
                    '}';
        }
    }

    public static class Resource {
        public String id;
        public String companyId;
        public String createTime;
        public String updateTime;
        public String name;
        public String fromVersion;
        public String toVersion;
        public String url;
        public String md5;
        public String productId;
        public Creator creator;
        public Updator updator;

    }

    public static class Creator {
        public String id;
        public String name;
    }

    public static class Updator {
        public String id;
        public String name;
    }

}